package com.hnichr.ump.build7.message.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.ump.build7.cache.PlateCache;
import com.hnichr.ump.build7.message.MessageArrivedHandle;
import com.hnichr.ump.build7.pojo.Plate;
import com.hnichr.ump.build7.service.StopService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.*;

@Component
public class IvsResultMessageArrivedHandle implements MessageArrivedHandle {
    private static final Logger logger = LoggerFactory.getLogger(IvsResultMessageArrivedHandle.class);

    @Autowired
    private StopService stopService;

    @Override
    public boolean support(String topic, String message) {
        return "b956ba98-988cd83e/device/message/up/ivs_result".equals(topic);
    }

    @Override
    public void handle(String topic, String message) {
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>车牌识别");
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            JSONObject payload = (JSONObject)jsonObject.get("payload");
            if(payload==null){
                return;
            }
            JSONObject alarmInfoPlate = (JSONObject)payload.get("AlarmInfoPlate");
            if(alarmInfoPlate==null){
                return;
            }
            JSONObject result = (JSONObject)alarmInfoPlate.get("result");
            if(result==null){
                return;
            }
            JSONObject plateResult = (JSONObject)result.get("PlateResult");
            if(plateResult==null){
                return;
            }
            String license = plateResult.getString("license");
            String plate =  new String(Base64.getDecoder().decode(license.getBytes(StandardCharsets.UTF_8)),StandardCharsets.UTF_8);
            String context = PlateCache.get(plate);
            if(context!=null){
                String msg = context+"["+plate+"]入场";
                logger.info(">>>>>>>>>>>>>>匹配成功：{}",msg);
                sendDDMessage(msg);
            }
            else{
                logger.info(">>>>>>>>>>>>>>匹配失败：{}非登记车辆",plate);
            }

        }
        catch (Exception e){
            logger.error("IvsResultMessageArrivedHandle",e);
        }
    }

    public  void sendDDMessage(String msg){
        String [] mobiles = new String[]{"18637109325","13673653174"};
        //String [] userIds = new String[]{"146121"};
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("type", "ddmessage");
        //paramMap.put("appId","SqZD8TSFSvWdx34igpKrVg");
        paramMap.put("appId", "y_Tuxn6PQHut6gBsThFsNw");
        paramMap.put("phones", mobiles);
        //paramMap.put("userIds", userIds);
        paramMap.put("content", msg);
        paramMap.put("msgtype","text");
        String postReturn = "";
        try (HttpResponse response = HttpUtil.createPost("http://api.zt.hnic.com.cn/hnic/v1/message/sendHnicMessage").contentType("application/json")
                .body(JSON.toJSONString(paramMap)).execute()) {
            postReturn = response.body();
        }
        catch (Exception e){
            postReturn = e.getMessage();
        }
        System.out.println("postReturn>>>>>>>>>"+postReturn);
    }
}
